import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: "/userlogin"


    }, {
        path: '/home',
        name: 'Home',
        component: () =>
            import ('../views/Home/Home.vue'),
        meta: {
            showTabBar: true
        },
        children: [{
            path: "index",
            name: "Index",
            component: () =>
                import ('../views/Home/children/Index.vue'),
            meta: {
                showTabBar: true
            }
        }, {
            path: "other",
            name: "Other",
            component: () =>
                import ('../views/Home/children/Other.vue'),
            meta: {
                showTabBar: true
            }
        }, {
            path: "/home",
            redirect: "/home/index"
        }]
    },
    {
        path: '/cart',
        name: 'Cart',

        component: () =>
            import ('../views/Cart/Cart.vue'),
        meta: {
            showTabBar: true
        }
    },
    {
        path: '/category',
        name: 'Category',

        component: () =>
            import ('../views/Category/Category.vue'),
        meta: {
            showTabBar: true
        }
    },
    {
        path: '/find',
        name: 'Find',

        component: () =>
            import ('../views/Find/Find.vue'),
        meta: {
            showTabBar: true
        }
    },
    {
        path: '/mine',
        name: 'Mine',

        component: () =>
            import ('../views/Mine/Mine.vue'),
        meta: {
            showTabBar: true
        }
    }, {
        path: '/userlogin',
        name: 'UserLogin',

        component: () =>
            import ('../views/Login/UserLogin.vue')
    }, {
        path: '/phonelogin',
        name: 'PhoneLogin',

        component: () =>
            import ('../views/Login/PhoneLogin.vue')
    },
    {
        path: '/userreqister',
        name: 'UserReqister',

        component: () =>
            import ('../views/Login/UserReqister.vue')
    },
    {
        path: '/categorylist/:cat_id',
        name: 'CategoryList',

        component: () =>
            import ('../views/Category/components/CategoryList.vue')
    }
]

const router = new VueRouter({
    routes
});
//路由守卫;
router.beforeEach((to, from, next) => {
    //to即将进入的目标
    //from即将离开的路由
    //next放行
    var token = localStorage.getItem('token');
    if (to.path == "/userlogin" || to.path == "/phonelogin" || to.path == "/userreqister") {
        next();
        return
    }
    if (!token) {
        //如果token不存在，直接重定向到登录页面
        return next('/userlogin')
    } else {
        next();
    }
})
export default router